//
//  AllForumCell.m
//  MoXiProject
//
//  Created by 沈晨宇 on 2017/7/31.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "AllForumCell.h"

@implementation AllForumCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        
        self.iconImage=[UIImageView new];
        self.iconImage.layer.cornerRadius = 25;
        self.iconImage.layer.masksToBounds = YES;
        [self.contentView addSubview:self.iconImage];
        
        self.name=[UILabel new];
        self.name.textColor=baseBlackColor;
        self.name.font=DEF_MyFont(16);
        self.name.numberOfLines=0;
        self.name.textAlignment=NSTextAlignmentLeft;
        [self.contentView addSubview:self.name];
        
        self.time=[UILabel new];
        self.time.textColor=baseLightBlackColor;
        self.time.font=DEF_MyFont(14);
        self.time.textAlignment=NSTextAlignmentLeft;
        [self.contentView addSubview:self.time];
        //
        //        self.comments=[UILabel new];
        //        self.comments.textColor=baseLightBlackColor;
        //        self.comments.font=DEF_MyFont(14);
        //        self.comments.numberOfLines=0;
        //        self.comments.textAlignment=NSTextAlignmentLeft;
        //        [self.contentView addSubview:self.comments];
        //
        self.report=[UIButton new];
        self.report.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14.f];
        [self.report setTitle:@"举报" forState:UIControlStateNormal];
        [self.report setTitleColor:DEF_COLOR_MAINCOLOR forState:UIControlStateNormal];
        [self.contentView addSubview:self.report];
        
        self.title=[UILabel new];
        self.title.textColor=baseBlackColor;
        self.title.font=DEF_MyFont(16);
        self.title.numberOfLines=0;
        self.title.textAlignment=NSTextAlignmentCenter;
        [self.contentView addSubview:self.title];
        
        self.content=[UILabel new];
        self.content.textColor=baseLightBlackColor;
        self.content.font=DEF_MyFont(14);
        self.content.numberOfLines=0;
        self.content.textAlignment=NSTextAlignmentLeft;
        [self.contentView addSubview:self.content];
        
        self.comment=[UILabel new];
        self.comment.textColor=baseLightBlackColor;
        self.comment.font=DEF_MyFont(14);
        self.comment.textAlignment=NSTextAlignmentRight;
        [self.contentView addSubview:self.comment];
        
        self.readNum=[UILabel new];
        self.readNum.textColor=baseLightBlackColor;
        self.readNum.font=DEF_MyFont(14);
        self.readNum.textAlignment=NSTextAlignmentRight;
        [self.contentView addSubview:self.readNum];
        
                self.line=[UIView new];
                self.line.backgroundColor=DEF_COLOR_F0F0F0;
                [self.contentView addSubview:self.line];
        
    }
    
    return self;
}

-(void)setModel:(AllForumModel *)model{
    
    _model=model;
    [self setFrame];
    [self setData];
}

-(void)setFrame{
    
    [self.iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(50);
        make.top.equalTo(self.contentView).with.offset(12);
        make.leading.equalTo(self.contentView).with.offset(15);
    }];
    
    [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(16);
        make.top.equalTo(self.contentView).with.offset(16);
        make.leading.equalTo(self.iconImage.mas_trailing).with.offset(10);
    }];
    
    [self.time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(14);
        make.top.equalTo(self.name.mas_bottom).with.offset(14);
        make.leading.equalTo(self.iconImage.mas_trailing).with.offset(10);
    }];
    
    [self.report mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(12);
        make.top.equalTo(self.contentView).with.offset(12);
        make.trailing.equalTo(self.contentView).with.offset(-15);
    }];
    
    CGFloat titleW =DEF_DEVICE_WIDTH-30;
    CGSize titleSize=[_model.title boundingRectWithSize:CGSizeMake(titleW, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName : DEF_MyFont(16)} context:nil].size;

    CGFloat titleX =15;
    CGFloat titleY = 50+12+10;
    CGFloat titleH = titleSize.height+1;
    self.title.frame=CGRectMake(titleX, titleY, titleW, titleH);
    
    [self.content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(DEF_DEVICE_WIDTH-30);
        make.height.mas_equalTo(34);
        make.top.equalTo(self.title.mas_bottom).with.offset(15);
        make.leading.equalTo(self.contentView).with.offset(15);
    }];
    
    [self.comment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(75);
        make.height.mas_equalTo(14);
        make.top.equalTo(self.content.mas_bottom).with.offset(30);
        make.trailing.equalTo(self.contentView).with.offset(-15);
    }];
    
    [self.readNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(75);
        make.height.mas_equalTo(14);
        make.centerY.equalTo(self.comment);
        make.trailing.equalTo(self.comment.mas_leading).with.offset(-5);
    }];
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(DEF_DEVICE_WIDTH-15);
            make.height.mas_equalTo(1);
            make.top.equalTo(self.readNum.mas_bottom).with.offset(12);
            make.leading.equalTo(self.contentView).with.offset(15);
        }];
  
    self.frame=CGRectMake(0, 0, 0, 50+12+10+titleSize.height+1+15+34+30+14+1+1+12);
    
}

-(void)setData{
    //
    
    self.title.text=_model.title;
    self.content.text=_model.content;
    self.comment.text=[NSString stringWithFormat:@"评论数:%@",_model.comments];
    self.readNum.text=[NSString stringWithFormat:@"阅读数:%@",_model.readNum];
    NSRange range = NSMakeRange(5,11);
    NSString* dateStr= [_model.createTime substringWithRange:range];
    self.name.text=_model.username;
    self.time.text=[NSString stringWithFormat:@"%@发布",dateStr];
    [self.iconImage sd_setImageWithURL:[NSURL URLWithString:_model.imgUrl]placeholderImage:DEF_NORMAL_DEFAULT_IMAGE];
    
}
-(NSString *)base64DecodeString:(NSString *)string
{
    
    //1.将base64编码后的字符串『解码』为二进制数据
    
    NSData *data = [[NSData alloc]initWithBase64EncodedString:string options:0];
    
    //2.把二进制数据转换为字符串返回
    
    return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    
}


@end
